package org.zhxy.core.quality.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zhxy.common.base.BaseService;
import org.zhxy.common.config.MapperFactory;
import org.zhxy.common.error.CommonException;
import org.zhxy.common.response.PageResult;
import org.zhxy.common.response.Result;
import org.zhxy.common.response.ResultCode;
import org.zhxy.common.utils.CollectionUtils;
import org.zhxy.common.utils.Constants;
import org.zhxy.entity.personnel.Teacher;
import org.zhxy.entity.principal.Stage;
import org.zhxy.entity.principal.SubSchool;
import org.zhxy.entity.principal.Vocational;
import org.zhxy.entity.quality.Classes;
import org.zhxy.entity.quality.Student;
import org.zhxy.entity.quality.StudentClasses;
import org.zhxy.entity.quality.UserStudent;
import org.zhxy.entity.system.User;

import java.util.List;
import java.util.Map;

@Service
@Transactional
public class StudentManagerService extends BaseService {

    public PageResult<UserStudent> getStudentList(Map<String, Object> map) throws CommonException {
        switch (currentUser().getLevel()){
            case Constants.System.ADMIN:
                List<UserStudent> list1 = MapperFactory.studentManagerMapper.searchAllStudentList(currentMap(map));
                this.handleList(list1);
                long total1 = MapperFactory.studentManagerMapper.getTotalCount(map);
                return new PageResult<UserStudent>(total1,list1);
            case Constants.System.USER:
                Teacher teacher = MapperFactory.teacherMapper.selectById(currentUser().getId());
                if(teacher.getIdentityName().equals("部门经理") || teacher.getIdentityName().equals("市场")){
                    List<UserStudent> list2 = MapperFactory.studentManagerMapper.searchAllStudentList(currentMap(map));
                    this.handleList(list2);
                    long total2 = MapperFactory.studentManagerMapper.getTotalCount(map);
                    return new PageResult<UserStudent>(total2,list2);
                }else{
                    List<UserStudent> list3= MapperFactory.studentManagerMapper.searchStudentListByTeacherClasses(currentMap(map));
                    this.handleList(list3);
                    long total3 = MapperFactory.studentManagerMapper.getTotalByTeacherClasses(map);
                    return new PageResult<UserStudent>(total3,list3);
                }
            default:
                throw new CommonException(ResultCode.FAIL);
        }
    }

    public void handleList(List<UserStudent> list){
        if(CollectionUtils.checkCollection(list)){
            for (UserStudent userStudent : list) {
                if(userStudent.getClassesId() != null){
                    Classes classes = MapperFactory.classesMapper.selectById(userStudent.getClassesId());
                    userStudent.setClassesName(classes.getClassesName());
                    userStudent.setStartTime(classes.getStartTime());
                    userStudent.setEndTime(classes.getEndTime());
                    String stageId = classes.getStageId();
                    Stage stage = MapperFactory.stageMapper.selectById(stageId);
                    userStudent.setStageName(stage.getLabel());
                    String vocationalId = stage.getVocationalId();
                    Vocational vocational = MapperFactory.vocationalMapper.selectById(vocationalId);
                    userStudent.setVocationalName(vocational.getLabel());
                    String subSchoolId = vocational.getSubSchoolId();
                    SubSchool subSchool = MapperFactory.subSchoolMapper.selectById(subSchoolId);
                    userStudent.setSubSchoolName(subSchool.getLabel());

                }
            }
        }
    }

    public Result replaceClassesByStudentId(Map<String, Object> map) {

        //todo:是否有考试. 如果有 则不允许

        String classesId = map.get(Constants.Parameters.CLASSES_ID).toString();
        String studentId = map.get(Constants.Parameters.STUDENT_ID).toString();

        Student student = MapperFactory.studentManagerMapper.selectById(studentId);
        student.setClassesId(classesId);
        MapperFactory.studentManagerMapper.updateById(student);

        QueryWrapper<StudentClasses> studentClassesQueryWrapper = new QueryWrapper<>();
        studentClassesQueryWrapper.eq(Constants.DB.STUDENT_ID,studentId);
        studentClassesQueryWrapper.eq(Constants.DB.TYPE,Constants.CharacterNumber.ONE);
        List<StudentClasses> studentClasses = MapperFactory.studentClassesMapper.selectList(studentClassesQueryWrapper);
        if(CollectionUtils.checkCollection(studentClasses)){
            for (StudentClasses studentClass : studentClasses) {
                studentClass.setType(Constants.CharacterNumber.TWO);
                MapperFactory.studentClassesMapper.updateById(studentClass);
            }
        }

        StudentClasses studentClass = StudentClasses.builder().studentId(studentId).classesId(classesId).type(Constants.CharacterNumber.ONE).build();
        MapperFactory.studentClassesMapper.insert(studentClass);

        return Result.SUCCESS();
    }
}
